#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <qprocess.h>
#include <qtimer.h>
#include <QAxObject>
#include "showret.h"
#include "usb_listener.h"
#include <QTableWidget>
#include <QSettings>
#include <QThread>
#include <QFile>
#include "readfile.h"
#include "wthread.h"
#include "areaconfig.h"
#include "pollthread.h"
#include "add_a_ch.h"
#include "add_d_ch.h"
#include "moddata.h"

//typedef struct dev {
//    QString ID;
//    int status;
//}DEV;


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    int openExcel(QString fileName);
    QVariant readAll(QAxObject *sheet);
    int castVariant2ListListVariant(const QVariant &var);
    void clear_file(QString filepath);

    int showinfofileexist();
    void ment_init();

    void newExcel(const QString &fileName);
    void setCellValue(int row, int column, const QString &value);
    void setCellValue(int row, int column, const int &value);

    void saveExcel(const QString &fileName);
    void Table2Excel(QTableWidget *table,QString title);
    void writetoExcel(QTableWidget *table,QString filename);
    void exportSlot(QTableWidget *table);
    void config_init();
    void update_table(int row);
    void table_init();
    void set_lable(QString str);
    void readpoll();
    void sendReadbroadcast();
    void Updatechannels(int row);

    void event_init();
    void setThreadImport(QString filename);
    void printStr(QString str);
    void updateFreqDisplay(void);

    void exportFile_fileNmae(QString fileName);
    QString value_to_index(QString type,QString value);
    QString index_to_value(QString type,QString index);

    QAxObject *pApplication;
    QAxObject *pWorkBooks;
    QAxObject *pWorkBook;
    QAxObject *pSheets;
    QAxObject *pSheet;


private slots:
    void on_pushButton_clicked();

    void onSearchFinished(int,QProcess::ExitStatus);
    void usb_deal();


    void on_send_clicked();

    int on_choose_file_clicked();

    void on_switch_file_clicked();

    //void on_choose_send_file_clicked();

    void control_disenable();

    void control_enable();

    int find_hash();
    void set_hash_deal(int exit_code);
    void hash_reset();
    void file_init();
    void hash_traversal();
    void on_connect_clicked();

    void on_disconnect_clicked();
    int wifidevice_exist(QString ID);
    void usb_listener_init();

    void on_pushButton_2_clicked();
    int writedatatofile();

    void RightClickSlot(QPoint pos);               //菜单点击，获取当前位置
    void RightClickDelete(QAction *act);             //得知菜单当前的位置并删除                        //得知菜单当前的位置
    void channelonTableClicked(const QModelIndex &index);

    void on_pushButton_3_clicked();

    void on_clean_clicked();

    void on_read_clicked();

    void recvId(int id);
    void exportFile();
    void importFile();
    void softdesc();
    void sendbroadcast();
    void deal_wclose();
    void areaconfig();

    void thread_dealOk();
    void thread_dealFailed1();
    void thread_dealFailed2();
    void thread_dealimport(int,QMap<int,CHANNINFO>*);

    void deal_wiatfor1s(int);
    void deal_timeout();
    void deal_pollfinish();
    void on_addarea_clicked();

    void on_cleanarea_clicked();

    void onTableClicked(const QModelIndex &index);
    void areaRightClickSlot(QPoint pos);
    void areaRightClickDelete(QAction *act);
    void update_areatable();
    void deal_add_d_ch(QString str);
    void deal_add_a_ch(QString str);
    void deal_moddate(QString str);

    void on_language_clicked();

    void on_freq_choose_clicked();

private:
    Ui::MainWindow *ui;
    QTimer *timer;
    QProcess *process;
    QString sdcard_filepath;
    QString dir_str;
    QString send_filename;
    int    adb_code;
    int    waitsec;
    QString     revdevID;
    QHash<QString,DEV> devices;
    int dealed_num;
    //int switch_flag;
    usb_listener *m_usb_listener;
    //usb_listenerh *m_usb_listener;
    //Readfile *rfile;
    QStringList headers;
    QStringList areaheaders;

    QMenu *RightClick;                          //右键点击
    QAction *deleteAction;                      //删除事件（如需要其它事件，可新定义）
    //int iDeletcRow;                        //删除事件（如需要其它事件，可新定义）

    QMenu *areaRightClick;
    QAction *selectAction;
    QAction *selectActionchannel;
    int selectRowchannel;

    int iselectcRow;
    int SelectRowID;
    int channelSelectRow;
    QString channelName;

    bool export_write;

    QMap<int,CHANNINFO> channels;
    QMap<int,CHANNINFO> channelsTemp;

    QMenuBar *menuBar;
    QMenu *fileMenu;
    QMenu *aboutMenu;
    QMenu *configMenu;
    QSettings *m_IniFile;

    QString pull_path;
    QString push_path;
    QString broadcast;
    //QString flagfile_path;
    QString readbroadcast;
    wThread *wTh;
    pollThread *pollTh;
    //areaConfig *areacfg;
    Readfile *rfile;
    //ADD_D_CH *d_ch;
    //ADD_A_CH *a_ch;
    QString language;
    QFile *log_file;

    int freq;
    QString freqValue;
};


#endif // MAINWINDOW_H
